package com.whale.cloud.util.jdbc;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.whale.cloud.util.StringUtils;
import lombok.extern.log4j.Log4j2;

/** Created by lide on 2017/7/4. */
@Log4j2
public class RelaKeyInfo {
  public static JSONObject getForeignKey(JSONObject object, JdbcClient client) {
    JSONArray array = object.getJSONArray("list");
    JSONArray array_ = new JSONArray();
    if (array.size() > 0) {
      for (int i = 0; i < array.size(); i++) {
        JSONObject obj = array.getJSONObject(i);
        //企业
        array_.add(getRelakeyobj(obj, client));
      }
    }
    JSONObject new_obj = new JSONObject();
    new_obj.put("data_count", object.getInteger("data_count"));
    new_obj.put("page_count", object.getInteger("page_count"));
    new_obj.put("list", array_);
    return new_obj;
  }

  public static JSONObject getRelakeyobj(JSONObject obj, JdbcClient jdbcClient) {
    if (obj.containsKey("def_corp_id")) {
      String def_corp_id = obj.getString("def_corp_id");
      if (StringUtils.isNotNull(def_corp_id)) {
        def_corp_id = StringUtils.getNewString(def_corp_id);
        String[] corp_id = def_corp_id.split("»«");
        if (corp_id.length > 0) {
          JSONArray _array = new JSONArray();
          for (int j = 0; j < corp_id.length; j++) {
            _array.add(
                getIdInfo(
                    corp_id[j], "def_corp", " id value,corp_name description ", "id", jdbcClient));
          }
          obj.put("def_corp_id", _array);
        }
      }
    }
    //region  用户多个企业的
    if (obj.containsKey("def_corp_ids")) {
      String def_corp_id = obj.getString("def_corp_ids");
      if (StringUtils.isNotNull(def_corp_id)) {
        def_corp_id = StringUtils.getNewString(def_corp_id);
        String[] corp_id = def_corp_id.split("»«");
        if (corp_id.length > 0) {
          JSONArray _array = new JSONArray();
          for (int j = 0; j < corp_id.length; j++) {
            _array.add(
                getIdInfo(
                    corp_id[j], "def_corp", " id value,corp_name description ", "id", jdbcClient));
          }
          obj.put("def_corp_ids", _array);
        }
      }
    }
    //endregion

    //region  多个门店
    if (obj.containsKey("applicablestore")) {
      String applicablestore = obj.getString("applicablestore");
      if (StringUtils.isNotNull(applicablestore)) {
        applicablestore = StringUtils.getNewString(applicablestore);
        String[] corp_id = applicablestore.split("»«");
        if (corp_id.length > 0) {
          JSONArray _array = new JSONArray();
          for (int j = 0; j < corp_id.length; j++) {
            _array.add(
                    getIdInfo(
                            corp_id[j], "def_store", " id value,store_name description ", "id", jdbcClient));
          }
          obj.put("applicablestore", _array);
        }
      }
    }
    //endregion

    //region  用户多个企业的
    if (obj.containsKey("def_corp_ids")) {
      String def_corp_id = obj.getString("def_corp_ids");
      if (StringUtils.isNotNull(def_corp_id)) {
        def_corp_id = StringUtils.getNewString(def_corp_id);
        String[] corp_id = def_corp_id.split("»«");
        if (corp_id.length > 0) {
          JSONArray _array = new JSONArray();
          for (int j = 0; j < corp_id.length; j++) {
            _array.add(
                getIdInfo(
                    corp_id[j], "def_corp", " id value,corp_name description ", "id", jdbcClient));
          }
          obj.put("def_corp_ids", _array);
        }
      }
    }
    //endregion

    //region  券类型定义
    if (obj.containsKey("def_coupon_id")) {
      String def_coupon_id = obj.getString("def_coupon_id");
      if (StringUtils.isNotNull(def_coupon_id)) {
        def_coupon_id = StringUtils.getNewString(def_coupon_id);
        String[] corp_id = def_coupon_id.split("»«");
        if (corp_id.length > 0) {
          JSONArray _array = new JSONArray();
          for (int j = 0; j < corp_id.length; j++) {
            _array.add(
                getIdInfo(
                    corp_id[j], "def_coupon", " id value,name description ", "id", jdbcClient));
          }
          obj.put("def_coupon_id", _array);
        }
      }
    }
    //endregion

    //region  用户所属角色
    if (obj.containsKey("def_role_ids")) {
      String def_corp_id = obj.getString("def_role_ids");
      if (StringUtils.isNotNull(def_corp_id)) {
        def_corp_id = StringUtils.getNewString(def_corp_id);
        String[] corp_id = def_corp_id.split("»«");
        if (corp_id.length > 0) {
          JSONArray _array = new JSONArray();
          for (int j = 0; j < corp_id.length; j++) {
            _array.add(
                getIdInfo(corp_id[j], "def_role", " id value,name description ", "id", jdbcClient));
          }
          obj.put("def_role_ids", _array);
        }
      }
    }
    //endregion

    if (obj.containsKey("def_store_ids")) {
      String def_corp_id = obj.getString("def_store_ids");
      if (StringUtils.isNotNull(def_corp_id)) {
        def_corp_id = StringUtils.getNewString(def_corp_id);
        String[] corp_id = def_corp_id.split("»«");
        if (corp_id.length > 0) {
          JSONArray _array = new JSONArray();
          for (int j = 0; j < corp_id.length; j++) {
            _array.add(
                getIdInfo(
                    corp_id[j],
                    "def_store",
                    " id value,store_name description ",
                    "id",
                    jdbcClient));
          }
          obj.put("def_store_ids", _array);
        }
      }
    }

    //品牌 多个
    if (obj.containsKey("def_brand_ids")) {
      String def_brand_ids = obj.getString("def_brand_ids");
      if (StringUtils.isNotNull(def_brand_ids)) {
        def_brand_ids = StringUtils.getNewString(def_brand_ids);
        String[] corp_id = def_brand_ids.split("»«");
        if (corp_id.length > 0) {
          JSONArray _array = new JSONArray();
          for (int j = 0; j < corp_id.length; j++) {
            _array.add(
                getIdInfo(
                    corp_id[j],
                    "def_brand",
                    " id value,brand_name description ",
                    "id",
                    jdbcClient));
          }
          obj.put("def_brand_ids", _array);
        }
      }
    }

    //品牌 单个
    if (obj.containsKey("def_brand_id")) {
      String def_brand_id = obj.getString("def_brand_id");
      if (StringUtils.isNotNull(def_brand_id)) {
        JSONArray _array = new JSONArray();
        _array.add(
            getIdInfo(
                def_brand_id, "def_brand", " id value,brand_name description ", "id", jdbcClient));
        obj.put("def_brand_id", _array);
      }
    }

    //记录创建人
    if (obj.containsKey("createid")) {
      String createid = obj.getString("createid");
      if (StringUtils.isNotNull(createid)) {
        JSONArray _array = new JSONArray();
        _array.add(
                getIdInfo(
                        createid, "def_user", " id value,user_name description ", "id", jdbcClient));
        obj.put("createid", _array);
      }
    }
    //记录修改人
    if (obj.containsKey("modifyid")) {
      String modifyid = obj.getString("modifyid");
      if (StringUtils.isNotNull(modifyid)) {
        JSONArray _array = new JSONArray();
        _array.add(
                getIdInfo(
                        modifyid, "def_user", " id value,user_name description ", "id", jdbcClient));
        obj.put("modifyid", _array);
      }
    }

    //文章
    if (obj.containsKey("def_wx_article_id")) {
      String def_wx_article_id = obj.getString("def_wx_article_id");
      if (StringUtils.isNotNull(def_wx_article_id)) {
        JSONArray _array = new JSONArray();
        _array.add(
                getIdInfo(
                        def_wx_article_id, "def_wx_article", " id value,name description ", "id", jdbcClient));
        obj.put("def_wx_article_id", _array);
      }
    }

    //签到规格
    if (obj.containsKey("def_sign_rule_id")) {
      String def_sign_rule_id = obj.getString("def_sign_rule_id");
      log.debug("-------->def_sign_rule_id"+def_sign_rule_id);
      if (StringUtils.isNotNull(def_sign_rule_id)) {
        JSONArray _array = new JSONArray();
        JSONObject _jo=getIdInfo(
                def_sign_rule_id, "def_sign_rule", " id value,sign_type description ", "id", jdbcClient);
        log.debug("--------jo66666:"+_jo);
        if(_jo!=null && _jo.containsKey("description")){
          if("EVERYDAY".equals(_jo.getString("description").trim())){
            _jo.put("description","每天");
          }else if("EVERYWEEK".equals(_jo.getString("description").trim())){
            _jo.put("description","每周");
          }
        }
        log.debug("----->_jo:"+_jo);
        _array.add(_jo);
        obj.put("def_sign_rule_id", _array);
      }
    }

    //消费区域
    if (obj.containsKey("def_customarea_id")) {
      String def_customarea_id = obj.getString("def_customarea_id");
      if (StringUtils.isNotNull(def_customarea_id)) {
        def_customarea_id = StringUtils.getNewString(def_customarea_id);
        String[] corp_id = def_customarea_id.split("»«");
        if (corp_id.length > 0) {
          JSONArray _array = new JSONArray();
          for (int j = 0; j < corp_id.length; j++) {
            _array.add(
                getIdInfo(
                    corp_id[j],
                    "def_custom_area",
                    " id value,custom_name description ",
                    "id",
                    jdbcClient));
          }
          obj.put("def_customarea_id", _array);
        }
      }
    }
    //省
    if (obj.containsKey("def_province_id")) {
      String def_province_id = obj.getString("def_province_id");
      if (StringUtils.isNotNull(def_province_id)) {
        def_province_id = StringUtils.getNewString(def_province_id);
        String[] corp_id = def_province_id.split("»«");
        if (corp_id.length > 0) {
          JSONArray _array = new JSONArray();
          for (int j = 0; j < corp_id.length; j++) {
            _array.add(
                getIdInfo(
                    corp_id[j],
                    "def_province",
                    " id value,shortname description ",
                    "id",
                    jdbcClient));
          }
          obj.put("def_province_id", _array);
        }
      }
    }
    //市
    if (obj.containsKey("def_city_id")) {
      String def_city_id = obj.getString("def_city_id");
      if (StringUtils.isNotNull(def_city_id)) {
        def_city_id = StringUtils.getNewString(def_city_id);
        String[] corp_id = def_city_id.split("»«");
        if (corp_id.length > 0) {
          JSONArray _array = new JSONArray();
          for (int j = 0; j < corp_id.length; j++) {
            _array.add(
                getIdInfo(
                    corp_id[j], "def_city", " id value,shortname description ", "id", jdbcClient));
          }
          obj.put("def_city_id", _array);
        }
      }
    }

    //区域
    if (obj.containsKey("def_area_id")) {
      String def_area_id = obj.getString("def_area_id");
      if (StringUtils.isNotNull(def_area_id)) {
        def_area_id = StringUtils.getNewString(def_area_id);
        String[] corp_id = def_area_id.split("»«");
        if (corp_id.length > 0) {
          JSONArray _array = new JSONArray();
          for (int j = 0; j < corp_id.length; j++) {
            _array.add(
                getIdInfo(
                    corp_id[j], "def_area", " id value,shortname description ", "id", jdbcClient));
          }
          obj.put("def_area_id", _array);
        }
      }
    }
    //经销商
    if (obj.containsKey("def_dealers_id")) {
      String def_dealers_id = obj.getString("def_dealers_id");
      if (StringUtils.isNotNull(def_dealers_id)) {
        def_dealers_id = StringUtils.getNewString(def_dealers_id);
        String[] corp_id = def_dealers_id.split("»«");
        if (corp_id.length > 0) {
          JSONArray _array = new JSONArray();
          for (int j = 0; j < corp_id.length; j++) {
            _array.add(
                getIdInfo(
                    corp_id[j],
                    "def_dealer",
                    " id value,dealers_name description ",
                    "id",
                    jdbcClient));
          }
          obj.put("def_dealers_id", _array);
        }
      }
    }
    if (obj.containsKey("def_storegroup_ids")) {
      String def_storegroup_ids = obj.getString("def_storegroup_ids");
      if (StringUtils.isNotNull(def_storegroup_ids)) {
        def_storegroup_ids = StringUtils.getNewString(def_storegroup_ids);
        String[] corp_id = def_storegroup_ids.split("»«");
        if (corp_id.length > 0) {
          JSONArray _array = new JSONArray();
          for (int j = 0; j < corp_id.length; j++) {
            _array.add(
                getIdInfo(
                    corp_id[j],
                    "def_store_group",
                    " id value,group_name description ",
                    "id",
                    jdbcClient));
          }
          obj.put("def_storegroup_ids", _array);
        }
      }
    }
    if (obj.containsKey("def_close_user_id")) {
      String def_close_user_id = obj.getString("def_close_user_id");
      if (StringUtils.isNotNull(def_close_user_id)) {
        def_close_user_id = StringUtils.getNewString(def_close_user_id);
        String[] corp_id = def_close_user_id.split("»«");
        if (corp_id.length > 0) {
          JSONArray _array = new JSONArray();
          for (int j = 0; j < corp_id.length; j++) {
            _array.add(
                getIdInfo(
                    corp_id[j], "def_user", " id value,user_name description ", "id", jdbcClient));
          }
          obj.put("def_close_user_id", _array);
        }
      }
    }
    if (obj.containsKey("def_user_id")) {
      String def_close_user_id = obj.getString("def_user_id");
      if (StringUtils.isNotNull(def_close_user_id)) {
        def_close_user_id = StringUtils.getNewString(def_close_user_id);
        String[] corp_id = def_close_user_id.split("»«");
        if (corp_id.length > 0) {
          JSONArray _array = new JSONArray();
          for (int j = 0; j < corp_id.length; j++) {
            _array.add(
                getIdInfo(
                    corp_id[j], "def_user", " id value,user_name description ", "id", jdbcClient));
          }
          obj.put("def_user_id", _array);
        }
      }
    }
    if (obj.containsKey("def_open_user_id")) {
      String def_open_user_id = obj.getString("def_open_user_id");
      if (StringUtils.isNotNull(def_open_user_id)) {
        def_open_user_id = StringUtils.getNewString(def_open_user_id);
        String[] corp_id = def_open_user_id.split("»«");
        if (corp_id.length > 0) {
          JSONArray _array = new JSONArray();
          for (int j = 0; j < corp_id.length; j++) {
            _array.add(
                getIdInfo(
                    corp_id[j], "def_user", " id value,user_name description ", "id", jdbcClient));
          }
          obj.put("def_open_user_id", _array);
        }
      }
    }
    if (obj.containsKey("def_customarea_id")) {
      String def_open_user_id = obj.getString("def_customarea_id");
      if (StringUtils.isNotNull(def_open_user_id)) {
        def_open_user_id = StringUtils.getNewString(def_open_user_id);
        String[] corp_id = def_open_user_id.split("»«");
        if (corp_id.length > 0) {
          JSONArray _array = new JSONArray();
          for (int j = 0; j < corp_id.length; j++) {
            _array.add(
                getIdInfo(corp_id[j], "def_area", " id value,name description ", "id", jdbcClient));
          }
          obj.put("def_customarea_id", _array);
        }
      }
    }
    if (obj.containsKey("def_user_ids")) {
      String def_user_ids = obj.getString("def_user_ids");
      if (StringUtils.isNotNull(def_user_ids)) {
        //def_user_ids=StringUtils.getNewString(erp_user_ids);
        def_user_ids = def_user_ids.replace("«", "");
        String[] corp_id = def_user_ids.split("»");
        if (corp_id.length > 0) {
          JSONArray _array = new JSONArray();
          for (int j = 0; j < corp_id.length; j++) {
            _array.add(
                getIdInfo(
                    corp_id[j], "def_user", " id value,user_name description ", "id", jdbcClient));
          }
          obj.put("def_user_ids", _array);
        }
      }
    }
    //会员类型多个
    if (obj.containsKey("def_vip_type_ids")) {
      String def_user_ids = obj.getString("def_vip_type_ids");
      if (StringUtils.isNotNull(def_user_ids)) {
        def_user_ids = def_user_ids.replace("«", "");
        String[] corp_id = def_user_ids.split("»");
        if (corp_id.length > 0) {
          JSONArray _array = new JSONArray();
          for (int j = 0; j < corp_id.length; j++) {
            _array.add(
                getIdInfo(
                    corp_id[j],
                    "def_vip_type",
                    " id value,vip_type_name description ",
                    "id",
                    jdbcClient));
          }
          obj.put("def_vip_type_ids", _array);
        }
      }
    }
    //公众号
    if (obj.containsKey("def_wx_public_id")) {
      String def_open_user_id = obj.getString("def_wx_public_id");
      if (StringUtils.isNotNull(def_open_user_id)) {
        def_open_user_id = StringUtils.getNewString(def_open_user_id);
        String[] corp_id = def_open_user_id.split("»«");
        if (corp_id.length > 0) {
          JSONArray _array = new JSONArray();
          for (int j = 0; j < corp_id.length; j++) {
            _array.add(
                getIdInfo(
                    corp_id[j],
                    "def_wx_public",
                    " id value,nick_name description ",
                    "id",
                    jdbcClient));
          }
          obj.put("def_wx_public_id", _array);
        }
      }
    }
    return obj;
  }

  public static JSONObject getIdInfo(
      String id, String table, String par, String by, JdbcClient Client) {
    String sql = "SELECT " + par + " FROM " + table + " where " + by + "=?";
    JSONObject obj = new JSONObject();
    obj = Client.getJSONObject(sql, new Object[] {id});
    if (null == obj) {
      return new JSONObject();
    }
    return obj;
  }

  public static String is_erp(String app_id, JdbcClient Client) {
    String sql =
        "SELECT a.param_value  FROM rel_corp_cfg a,def_corp_cfg b where a.DEF_CORP_CFG_ID=b.id  and b.PARAM_VALUES='IS_ERP' and a.DEF_PUBLIC_ID=?  and a.IS_ACTIVE='Y' and b.IS_ACTIVE='Y'";
    JSONArray obj = new JSONArray();
    obj = Client.getJSONArray(sql, new Object[] {app_id});
    if (obj.size() > 0) {
      log.info(obj);
      return obj.getJSONObject(0).getString("param_value");
    }
    return "";
  }
}
